﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace FlexiPay
{
    public partial class ManageData : System.Web.UI.Page
    {
        #region manage display
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                manageDisplayTabs();
                manageGrids();
            }
        }

        protected void manageDisplayTabs()
        {
            String tableName = DataUploadVariables.Table4DataUpload;

            TabData_Sales.Enabled = tableName.Equals("CarSalesData");
            TabData_Sales.HeaderText = tableName.Equals("CarSalesData") ? "Upload Data" : String.Empty;

            TabCityCategory.Enabled = tableName.Equals("CityCategory");
            TabCityCategory.HeaderText = tableName.Equals("CityCategory") ? "Upload Data" : String.Empty;

            TabDMAMaster.Enabled = tableName.Equals("DMAMaster");
            TabDMAMaster.HeaderText = tableName.Equals("DMAMaster") ? "Upload Data" : String.Empty;

            TabSegment.Enabled = tableName.Equals("Segments");
            TabSegment.HeaderText = tableName.Equals("Segments") ? "Upload Data" : String.Empty;

            TabSource.Enabled = tableName.Equals("Source");
            TabSource.HeaderText = tableName.Equals("Source") ? "Upload Data" : String.Empty;
            
            TabTaxMaster.Enabled = tableName.Equals("TaxMaster");
            TabTaxMaster.HeaderText = tableName.Equals("TaxMaster") ? "Upload Data" : String.Empty;
            
            TabGenTable.Enabled = DataUploadVariables.GenTableList.Contains(tableName);
            TabGenTable.HeaderText = DataUploadVariables.GenTableList.Contains(tableName) ? "Upload Data" : String.Empty;

            return;
        }

        protected void manageGrids()
        {
            String tableName = DataUploadVariables.Table4DataUpload;
            // sales data
            JQGridSalesInput.DataSource = tableName.Equals("CarSalesData") ? DataUploadVariables.UploadData : null;
            JQGridSalesInput.DataBind();
            // city category
            JQGridCityCategory.DataSource = tableName.Equals("CityCategory") ? DataUploadVariables.UploadData : null;
            JQGridCityCategory.DataBind();
            //DMA Master
            JQGridDMAMaster.DataSource = tableName.Equals("DMAMaster") ? DataUploadVariables.UploadData : null;
            JQGridDMAMaster.DataBind();
            //Segment
            JQGridSegment.DataSource = tableName.Equals("Segments") ? DataUploadVariables.UploadData : null;
            JQGridSegment.DataBind();
            //Generic Tables = { "Employers", "SchemeCode", "PromotionScheme", "ChannelCode", "MOP" }
            JQGridGenTable.DataSource = DataUploadVariables.GenTableList.Contains(tableName) ? DataUploadVariables.UploadData : null;
            JQGridGenTable.DataBind();
            //Source
            JQGridSource.DataSource = tableName.Equals("Source") ? DataUploadVariables.UploadData : null;
            JQGridSource.DataBind();
            //TaxMaster
            JQGRidTaxMaster.DataSource = tableName.Equals("TaxMaster") ? DataUploadVariables.UploadData : null;
            JQGRidTaxMaster.DataBind();

        }

        protected void ButtonViewData_Click(object sender, EventArgs e)
        {
            manageDisplayTabs();
        }

        protected void JQGrid_DataRequesting(object sender, Trirand.Web.UI.WebControls.JQGridDataRequestEventArgs e)
        {
            manageGrids();
        }

        protected void TableListDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataUploadVariables.Table4DataUpload = TableListDropDown.SelectedValue;
            manageDisplayTabs();
        }

        protected void TableListDropDown_DataBound(object sender, EventArgs e)
        {
            TableListDropDown.SelectedValue = DataUploadVariables.Table4DataUpload;
        }
        #endregion

        #region perform data operations
        //###################### GENERIC TABLE ########################
        protected void OnRowAdding_GenTable(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            e.RowData.Add("Category", "GenTable");
            e.RowData.Add("Name", TableListDropDown.SelectedValue);
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());

            SDSGenTable.InsertCommand = DataBaseMethods.generateInsertQuery("GenericTable", e.RowData);
            return;
        }

        protected void OnRowEditing_GenTable(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());
            List<String> keys = new List<String> { "RowNo" };

            SDSGenTable.UpdateCommand = DataBaseMethods.generateUpdateQuery("GenericTable", e.RowData, keys);
            return;
        }

        protected void OnRowDeleting_GenTable(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            SDSGenTable.DeleteCommand = DataBaseMethods.generateDeleteQuery("GenericTable", "RowNo", e.RowKey);
            return;
        }

        //###################### SOURCE TABLE ########################
        protected void OnRowAdding_Source(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());

            SDSSource.InsertCommand = DataBaseMethods.generateInsertQuery("Source", e.RowData);
            return;
        }

        protected void OnRowEditing_Source(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());
            List<String> keys = new List<String> { "RowNo" };

            SDSSource.UpdateCommand = DataBaseMethods.generateUpdateQuery("Source", e.RowData, keys);
            return;
        }

        protected void OnRowDeleting_Source(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            SDSSource.DeleteCommand = DataBaseMethods.generateDeleteQuery("Source", "RowNo", e.RowKey);
            return;
        }

        //###################### SEGMENTS TABLE ########################
        protected void OnRowAdding_Segments(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());

            SDSSegment.InsertCommand = DataBaseMethods.generateInsertQuery("Segments", e.RowData);
            return;
        }

        protected void OnRowEditing_Segments(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());
            List<String> keys = new List<String> { "RowNo" };

            SDSSegment.UpdateCommand = DataBaseMethods.generateUpdateQuery("Segments", e.RowData, keys);
            return;
        }

        protected void OnRowDeleting_Segments(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            SDSSegment.DeleteCommand = DataBaseMethods.generateDeleteQuery("Segments", "RowNo", e.RowKey);
            return;
        }

        //###################### DMA Master TABLE ########################
        protected void OnRowAdding_DMAMaster(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());

            SDSDMAMaster.InsertCommand = DataBaseMethods.generateInsertQuery("DMAMaster", e.RowData);
            return;
        }

        protected void OnRowEditing_DMAMaster(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());
            List<String> keys = new List<String> { "BrokerCode" };

            SDSDMAMaster.UpdateCommand = DataBaseMethods.generateUpdateQuery("DMAMaster", e.RowData, keys);
            return;
        }

        protected void OnRowDeleting_DMAMaster(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            SDSDMAMaster.DeleteCommand = DataBaseMethods.generateDeleteQuery("DMAMaster", "BrokerCode", e.RowKey);
            return;
        }

        //###################### CITY CATEGORY TABLE ########################
        protected void OnRowAdding_CityCat(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());

            SDSCityCategory.InsertCommand = DataBaseMethods.generateInsertQuery("CityCategory", e.RowData);
            return;
        }

        protected void OnRowEditing_CityCat(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());
            List<String> keys = new List<String> { "RowNo" };

            SDSCityCategory.UpdateCommand = DataBaseMethods.generateUpdateQuery("CityCategory", e.RowData, keys);
            return;
        }

        protected void OnRowDeleting_CityCat(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            SDSCityCategory.DeleteCommand = DataBaseMethods.generateDeleteQuery("CityCategory", "RowNo", e.RowKey);
            return;
        }

        //###################### SALES DATA TABLE ########################
        protected void OnRowAdding_SalesData(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());

            SDSSalesData.InsertCommand = DataBaseMethods.generateInsertQuery("CarSalesData", e.RowData);
            return;
        }

        protected void OnRowEditing_SalesData(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            e.RowData.Add("ModifiedBy", GlobalVariables.loginId);
            e.RowData.Add("ModifiedOn", System.DateTime.Now.ToString());
            List<String> keys = new List<String> { "AGREEMENTID" };

            SDSSalesData.UpdateCommand = DataBaseMethods.generateUpdateQuery("CarSalesData", e.RowData, keys);
            return;
        }

        protected void OnRowDeleting_SalesData(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            NameValueCollection nvc = new NameValueCollection(); 
            nvc.Add("EntryStatus", "deleted");
            nvc.Add("AGREEMENTID", e.RowKey.ToString());
            List<String> keys = new List<String> { "AGREEMENTID" };

            //delete will set status deleted
            SDSSalesData.DeleteCommand = DataBaseMethods.generateUpdateQuery("CarSalesData", nvc, keys);
            return;
        }

        //###################### TAX MATER TABLE ########################
        protected void OnRowAdding_TaxMaster(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());

            SDSCityCategory.InsertCommand = DataBaseMethods.generateInsertQuery("TaxMaster", e.RowData);
            return;
        }

        protected void OnRowEditing_TaxMaster(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            e.RowData.Add("CreatedBy", GlobalVariables.loginId);
            e.RowData.Add("CreatedOn", System.DateTime.Now.ToString());
            List<String> keys = new List<String> { "RowNo" };

            SDSCityCategory.UpdateCommand = DataBaseMethods.generateUpdateQuery("TaxMaster", e.RowData, keys);
            return;
        }

        protected void OnRowDeleting_TaxMaster(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            SDSCityCategory.DeleteCommand = DataBaseMethods.generateDeleteQuery("TaxMaster", "RowNo", e.RowKey);
            return;
        }

        #endregion
    }
}